package FanXingT;

import FanXingT.fclass.Apple;
import FanXingT.fclass.Basket;
import FanXingT.fclass.Fruit;
import FanXingT.fclass.Orange;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ajie
 * @date 2022/11/24
 * @function 集合泛型测试
 */
public class ListFanXing {
    private static final Logger logger = Logger.getLogger(ListFanXing.class);

    @Test
    public void ListT() {
      System.out.println("ListT:");
       // ArrayList<Fruit> fruits = new ArrayList <Apple>();//编译不过。泛型统一
        // fruits.add (new Orange());
    }

    @Test
    public void brokenTSafe() {
      System.out.println("brokenTSafe:");
        List <Basket<Fruit>> fruitBasket = new ArrayList<>();
        //编译error.  泛型不可变。
        //List<Object> objs = fruitBasket;//编译器：Required type: List <Object> Provided: List<Fruit>

        //编译：OK！
        //运行：java.lang.ClassCastException
        //为了兼容Java1.5以下的代码，编译期允许不指定泛型。
        List objs = fruitBasket; // 为了兼容性，编译器不强制要求指定泛型
        objs.add ("not a fruit !");
        Basket basket = fruitBasket.get(0);//运行：java.lang.ClassCastException
    }
}
